package cn.chinasyq.photoquan;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.RadioGroup;

import java.util.ArrayList;

import cn.chinasyq.photoquan.base.FragmentTabAdapter;
import cn.chinasyq.photoquan.base.OnFragmentShowListener;
import cn.chinasyq.photoquan.photo.PhotoSearchActivity;
import cn.chinasyq.photoquan.photo.fragment.GamePhotoListFragment;
import cn.chinasyq.photoquan.view.NotLoadedViewPager;
import cn.chinasyq.photoquan.view.StillViewPager;
import cn.chinasyq.photoquan.view.ViewPagerTitleIndicator.TabInfo;
import cn.master.volley.commons.VolleyHelper;

public class GameIndexActivity extends FragmentActivity implements NotLoadedViewPager.OnPageChangeListener, View.OnClickListener {

    protected int mCurrentTab = 0;
    protected int mLastTab = -1;
    // viewpager
    protected StillViewPager mPager;
    // 存放选项卡信息的列表
    protected ArrayList<TabInfo> mTabs = new ArrayList<>();
    private RadioGroup group;

    @Override
    protected void onResume() {
        super.onResume();
        if (adapter != null && adapter.getItem(mCurrentTab) != null && adapter.getItem(mCurrentTab) instanceof OnFragmentShowListener) {
            ((OnFragmentShowListener) adapter.getItem(mCurrentTab)).onShow();
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game_index);
        VolleyHelper.init(this);
        initView();
        initTab();
    }


    private void initView() {
        // 这里初始化界面
        mPager = (StillViewPager) findViewById(R.id.pager_matchfunction);
        group = (RadioGroup) findViewById(R.id.group);
        group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId != icons[mCurrentTab]) {
                    if (checkedId == icons[0]) {
                        select(0);
                    } else {
                        select(1);
                    }
                }
            }
        });
        mPager.setScanScroll(false);
        mPager.setOnPageChangeListener(this);
        mLastTab = mCurrentTab;
    }

    @Override
    public void onPageScrolled(int position, float positionOffset,
                               int positionOffsetPixels) {
    }

    @Override
    public void onPageSelected(int position) {
        group.check(icons[position]);
        mCurrentTab = position;
        if (adapter.getItem(position) instanceof OnFragmentShowListener) {
            ((OnFragmentShowListener) adapter.getItem(position)).onShow();
        }
        hideSoftKeyboard();
    }

    @Override
    public void onPageScrollStateChanged(int state) {
        if (state == ViewPager.SCROLL_STATE_IDLE) {
            mLastTab = mCurrentTab;
        }
    }

    public void select(int po) {
        mPager.setCurrentItem(po);
    }

    private void initTab() {
        Bundle bundle =new Bundle(getIntent().getExtras());
        bundle.putString("sort_type", "date");
        mTabs.add(new TabInfo(FRAGMENT_ONE, CONTENT[0], icons[0], fragments[0], bundle));
        bundle =new Bundle(getIntent().getExtras());
        bundle.putString("sort_type", "vote_count");
        mTabs.add(new TabInfo(FRAGMENT_TWO, CONTENT[1], icons[1], fragments[1], bundle));

        mCurrentTab = FRAGMENT_ONE;
        adapter = new FragmentTabAdapter(this,
                getSupportFragmentManager(), mTabs);
        mPager.setAdapter(adapter);

    }

    public static final int FRAGMENT_ONE = 0;
    public static final int FRAGMENT_TWO = 1;
    private static String[] CONTENT = {"影展", "我"};
    private static int[] icons = {R.id.radioButton, R.id.radioButton2};
    private static Class[] fragments = {GamePhotoListFragment.class, GamePhotoListFragment.class};
    private FragmentTabAdapter adapter;

    /**
     * 关闭软键盘
     *
     * @return
     */
    public void hideSoftKeyboard() {
        // 当点击空白区域，隐藏软键盘
        if (getCurrentFocus() != null && getCurrentFocus().getWindowToken() != null) {
            InputMethodManager manager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            manager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ibtn_back:
                onBackPressed();
                break;
            case R.id.ibtn_search:
                Intent i = new Intent(getIntent());
                i.setClass(getApplicationContext(), PhotoSearchActivity.class);
                startActivity(i);
                break;
        }
    }

}
